﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

using CE.Provider;
using CE.Image;
using CE.Global;

public partial class Admin_provider_UpdateProviderLogo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.SiteAdmin))
            {
                Response.Redirect("~/Admin/AdminSecureLogin.aspx");
            }
        }
        else
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            DataTable _dtCompany = General.GetComboItem("TH.ProviderCompany", false);
            ddlProviderCompanyName.DataSource = _dtCompany;
            ddlProviderCompanyName.DataTextField = _dtCompany.Columns[0].ColumnName.ToString();
            ddlProviderCompanyName.DataValueField = _dtCompany.Columns[1].ColumnName.ToString();
            ddlProviderCompanyName.DataBind();

            ddlProviderCompanyName.Items.Insert(0, new ListItem("Select", "-1"));
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //txtLogoFilePath.Text = fupCompanyLogo.PostedFile.FileName;
        if (fleUpload.PostedFile != null)
        {
            HttpPostedFile myFile = fleUpload.PostedFile;
            int fileSize = myFile.ContentLength;

            if (fileSize > 0 && (fileSize / 1024) < 100)
            {
                CompanyLogo _comlogo = new CompanyLogo();
                CompanyDB _comdb = new CompanyDB();
                //string imgPath = ConfigurationManager.AppSettings.Get("UploadImagePath");

                MyImage myImage = new MyImage();
                // Allocate a buffer for reading of the file
                byte[] myData = new byte[fileSize];

                // Read uploaded file from the Stream
                myFile.InputStream.Read(myData, 0, fileSize);

                // Create a name for the file to store
                string strFilename = Path.GetFileName(myFile.FileName);
                string strUploadLogoPath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.UploadCompanyLogoPath");
                string uploadPath = Server.MapPath(strUploadLogoPath + strFilename);
                //string companylogoPath = Server.MapPath("~/admin/providercompanylogo/" + strFilename);
                // Write data into a file
                myImage.WriteToFile(uploadPath, ref myData);
                imgCompanyLogo.ImageUrl = strUploadLogoPath + strFilename;
                _comlogo.CompanyLogoServerUrl = strUploadLogoPath + strFilename;
                //_comlogo.CompanyLogoSmall1 = MyImage.ReadImageFile(uploadPath, companylogoPath, 400, 150);
                _comlogo.CompanyLogoSmall1 = myData;

                _comlogo.ProviderCompanyId = Convert.ToInt64(ddlProviderCompanyName.Text);
                _comlogo.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());

                if (_comdb.UpdateLogo(_comlogo) > 0)
                {
                    _comlogo.ProviderCompanyId = 0;
                    lblServerUrl.Text = _comlogo.CompanyLogoServerUrl;
                    
                }
                //fupCompanyLogo.SaveAs(Server.MapPath("~\\ImageTemp\\" + fupCompanyLogo.FileName));
                //byte[] ImageFile = JobBaseImage.ReadImageFile(fupCompanyLogo.FileName);
            }
            else
            {
                lblMessage.Text = "File size should be under 100 KB.";
            }
        }



    }


    protected void ddlProviderCompanyName_SelectedIndexChanged(object sender, EventArgs e)
    {
        CompanyLogo _comlogo = new CompanyLogo();
        CompanyDB _comdb = new CompanyDB();
        _comlogo = _comdb.GetCompanyLogo(Convert.ToInt64(ddlProviderCompanyName.Text));
        if (_comlogo != null)
        {
            //Show Company Image
            //Create Temp Image Name from CompanyId+CompanyDescId
            string tempImageName = ddlProviderCompanyName.SelectedItem.Text + "_logo.gif";
            //Create Physical file path
            string filePath = Server.MapPath("~/captcha/" + tempImageName);

            #region Temp
            //byte[] fileData = myComDesc.CompanyLogo;
            //const int myBufferSize = 1024;
            //Stream myInputStream = new MemoryStream(fileData);
            //Stream myOutputStream = System.IO.File.OpenWrite(filePath);

            //byte[] buffer = new Byte[myBufferSize];
            //int numbytes;
            //while ((numbytes = myInputStream.Read(buffer, 0, myBufferSize)) > 0)
            //{
            //    myOutputStream.Write(buffer, 0, numbytes);
            //}
            //myInputStream.Close();
            //myOutputStream.Close();
            #endregion
            if (_comlogo.CompanyLogoServerUrl != String.Empty)
            {
                lblServerUrl.Text = _comlogo.CompanyLogoServerUrl;
                imgCompanyLogo.ImageUrl = _comlogo.CompanyLogoServerUrl;
            }

            /* Read from SQL Server Data
            if (_comlogo.CompanyLogoSmall1 != null)
            {
                imgCompanyLogo.Visible = true;
                string retImageUrl = MyImage.ReadImageData(_comlogo.CompanyLogoSmall1, filePath, tempImageName);
                if (retImageUrl != String.Empty)
                {
                    imgCompanyLogo.ImageUrl = retImageUrl;
                }
                else
                {
                    imgCompanyLogo.ImageUrl = "~/captcha/new_company_logo.gif";
                }
            }
            else
            {
                imgCompanyLogo.ImageUrl = "~/captcha/new_company_logo.gif";
            }*/

        }
        else
        {
            imgCompanyLogo.ImageUrl = "~/captcha/new_company_logo.gif";
        }

    }

    protected void btnUpdateScrollingLogo_Click(object sender, EventArgs e)
    {
        Response.Redirect("UpdateScrollingProviderLogo.aspx");
    }
}
